package com.hb.trade.server.project.evaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.project.evaluation.dto.EvaluationReviewItemParam;
import com.hb.core.pojo.project.evaluation.entity.EvaluationSecond;
import com.hb.core.pojo.project.evaluation.entity.EvaluationSummary;
import com.hb.core.pojo.project.evaluation.mapper.EvaluationSecondMapper;
import com.hb.core.pojo.project.evaluation.mapper.EvaluationSummaryMapper;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.core.pojo.project.section.mapper.BidSectionMapper;
import com.hb.trade.server.project.evaluation.service.EvaluationSummaryService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 评审总表 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2022-04-13
 */
@Service
public class EvaluationSummaryServiceImpl extends ServiceImpl<EvaluationSummaryMapper, EvaluationSummary> implements EvaluationSummaryService {

    @Resource
    private EvaluationSecondMapper evaluationSecondMapper;
    @Resource
    private BidSectionMapper bidSectionMapper;

    /**
     * 结束评标
     * @author tian
     * @date 2022-4-20 17:45
     * @param evaluationReviewItemParam
     * @return org.fzzn.core.model.msg.Message<java.lang.String>
     */
    @Override
    public Message<String> endReview(EvaluationReviewItemParam evaluationReviewItemParam) {
        // 查询二级评审是否全部已汇总
        QueryWrapper<EvaluationSecond> wrapper = EvaluationSecond.wrapper();
        wrapper.eq("section_id", evaluationReviewItemParam.getSectionId())
                .ne("evaluation_status", "2");
        Long reviewSecond = evaluationSecondMapper.selectCount(wrapper);

        if (reviewSecond != 0) {
            return MsgUtil.fail("当前有未汇总的评审项，请先去汇总");
        }

        EvaluationSummary evaluationSummary = new EvaluationSummary();
        evaluationSummary.setEvaluationStatus(2);

        QueryWrapper updateWrapper = new QueryWrapper();
        updateWrapper.eq("section_id", evaluationReviewItemParam.getSectionId());

        int num = baseMapper.update(evaluationSummary, updateWrapper);
        return num > 0 ? MsgUtil.success() : MsgUtil.fail("结束评标失败");
    }



    @Override
    public EvaluationSummary getEvaluationSummaryBySectionId(Long sectionId) {
        QueryWrapper<EvaluationSummary> wrapper = EvaluationSummary.wrapper();
        wrapper.eq("section_id",sectionId).last("limit 1");
        return getOne(wrapper);
    }

    /**
     * 初始化评审总表
     *
     * @param sectionId
     * @author tian
     * @date 2022-4-24 17:13
     */
    @Override
    public void initEvaluationSummary(Long sectionId) {
        QueryWrapper<EvaluationSummary> wrapper = EvaluationSummary.wrapper();
        wrapper.eq("section_id", sectionId).last("limit 1");
        EvaluationSummary evaluationSummary = getOne(wrapper);
        if (null == evaluationSummary) {
            // 查询标段绑定的招标文件
            BidSection bidSection = bidSectionMapper.selectById(sectionId);

            evaluationSummary = new EvaluationSummary();
            evaluationSummary.buildBaseData(1);
            evaluationSummary.setSectionId(sectionId);
            evaluationSummary.setFileId(bidSection.getFileId());
            evaluationSummary.setEvaluationStatus(1);
            baseMapper.insert(evaluationSummary);
        }
    }
}
